package com.buyouchina.service.product.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.springframework.stereotype.Service;

import com.buyouchina.bean.product.OrderForm;
import com.buyouchina.bean.product.Outlets;
import com.buyouchina.bean.product.OrderForm.Type;
import com.buyouchina.bean.product.Outlets.OutletsType;
import com.buyouchina.service.base.DaoSupport;
import com.buyouchina.service.product.OrderService;

@Service
public class OrderServiceBean extends DaoSupport<OrderForm> implements
		OrderService {

	String hql;
	
	
	public Integer getCount(Integer pid,Integer lastPid){
		hql="select count(o) from OrderForm o where o.province.id between ?1 and ?2";
		Object object=em.createQuery(hql).setParameter(1, pid).setParameter(2, lastPid).getSingleResult();
		return ((Long) object).intValue();
	}
	
	public Integer getCountByOid(Outlets ot){
		if(ot.getType().equals(OutletsType.WAREHOUSE)){
			hql="select count(od) from OrderForm od inner join od.from ou where ou.id=?1";
		}else{
			hql="select count(od) from OrderForm od inner join od.outlets ou where ou.id=?1";
		}
		Object object=em.createQuery(hql).setParameter(1, ot.getId()).getSingleResult();
		return ((Long) object).intValue();
	}
	
	public List<OrderForm> findByOid(Integer oid,Integer firstResult,Integer maxResults) {
		hql = "select od from OrderForm od inner join od.outlets ou where ou.id=?1";
		return list(hql, firstResult, maxResults, oid);
	}
	
	public List<OrderForm> findByOid(Integer oid) {
		hql = "select od from OrderForm od inner join od.outlets ou where ou.id=?1";
		return list(hql,oid);
	}

	public List<OrderForm> findByStatus(String status) {
		hql = "from OrderForm od where od.status=?1";
		return list(hql, status);
	}

	public List<OrderForm> findByDate(Date startTime,Date endTime) {
		hql = "from OrderForm od where od.date between ?1 and ?2";
		return list(hql, startTime,endTime);
	}
	
	public List<OrderForm> list(Outlets ot,Type type,Integer firstResult,Integer maxResults) {
		if(ot.getType().equals(OutletsType.WAREHOUSE)){
			hql = "select od from OrderForm od inner join od.from ou where ou.id=?1 and od.type=?2";
		}else if(ot.getType().equals(OutletsType.STORE)){
			hql = "select od from OrderForm od where od.from.id=?1 or od.outlets.id=?2 and od.type=?3";
			return list(hql, firstResult, maxResults, ot.getId(),ot.getId(),type);
		}
		else{
			hql = "select od from OrderForm od inner join od.outlets ou where ou.id=?1 and od.type=?2";
		}
		return list(hql, firstResult, maxResults, ot.getId(),type);
	}
	
	
	public List<OrderForm> list(List<Outlets> ots,Integer firstResult,Integer maxResults) {
		List<Object> params=new ArrayList<Object>();
		hql = "select od from OrderForm od inner join od.from ou where od.type=PURCHASESORDER and ou.id in (";
		for(int i=0;i<ots.size();i++){
			if(i==ots.size()){
				hql+="?"+i+")";
			}
			hql+="?"+i+" ";
			params.add(ots.get(i).getId());
		}
		return list(hql, firstResult, maxResults, params.toArray());
	}
	
	public List<OrderForm> list(List<Outlets> ots) {
		List<Object> params=new ArrayList<Object>();
		hql = "select od from OrderForm od inner join od.from ou where od.type=PURCHASESORDER and ou.id in (";
		if(ots.size()==1){
			hql = "select od from OrderForm od inner join od.outlets ou where od.type=?1 and ou.id=?2";
			params.add(Type.PURCHASESORDER);
			params.add(ots.get(0).getId());
		}else if(ots.size()>1){
			for(int i=0;i<ots.size();i++){
				if(i==ots.size()-1){
					hql+="?"+(i+1)+")";
				}else{
					hql+="?"+(i+1)+",";
				}
				params.add(ots.get(i).getId());
			}
		}else{
			return null;
		}
		return list(hql, params.toArray());
	}

}
